package com.earogya.logic.pharmacy.request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.struts.pharmacy.pharmacy.model.*;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.group.*;
import ca.uhn.hl7v2.model.v251.message.TBR_R08;
import ca.uhn.hl7v2.model.v251.segment.PID;

public class TBR_R08_Handler {
	private TBR_R08 adtMsg;
	public TBR_R08_Handler(){
		
	}
	
	public TBR_R08_Handler(TBR_R08 adtMsg){
		this.adtMsg=adtMsg;
	}
	
	public List<DrugsGivenToPatient> manageMessage() throws HL7Exception{
		List <DrugsGivenToPatient> drugsList = new ArrayList<DrugsGivenToPatient>();
		for(int i=0; i <adtMsg.getRDTReps();i++){
			String sMessage = adtMsg.getRDT(i).getColumnValue().encode();
			String saMessage[] = sMessage.split("  ");
			DrugsGivenToPatient drugs = new DrugsGivenToPatient();
			drugs.setDispenceAmountUnits(saMessage[5]);
			drugs.setDispenceDate(saMessage[8]);
			//drugs.setDispencedByID(id);
			drugs.setDispencedQuantity(saMessage[6]);
			//drugs.setDispenceId(rid);
			drugs.setDosage(saMessage[7]);
			drugs.setDrugId(saMessage[1]);
			drugs.setDrugName(saMessage[2]);
			drugs.setPatientId(saMessage[0]);
			drugs.setPrescriptionNo(saMessage[0]);
			drugsList.add(drugs);
			
		}
		return drugsList;
	}
}
